package com.longxi.tianjintianbao.ui.activity.assetrepair;

import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.model.assetrepair.AssetRepair;
import com.longxi.tianjintianbao.ui.adapter.multitype.assetrepair.AssetRepairViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.NListRefresh;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by cxw on & 2018/12/15
 */
public class AssetRepairActivity extends BaseActivity {

    private MultiTypeAdapter adapter;
    private Items items = new Items();
    private int page = 0;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_general_refresh_list;
    }

    @Override
    public void initView() {
        super.initView();
        setTvLeft("返回");
        setTitle("资产维修");
        initAdapter();
    }

    private void initAdapter() {
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            adapter.register(AssetRepair.class, new AssetRepairViewBinder(new AssetRepairViewBinder.OnClickListener() {
                @Override
                public void onClick(int position) {
                }
            }));
            mRvContent.setAdapter(adapter);
            adapter.setItems(items);
        }
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void initListener() {
        super.initListener();
        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                getAssetsRepair();
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page=0;
                getAssetsRepair();
            }
        });
        mRefreshLayout.autoRefresh();
    }

    private void getAssetsRepair(){
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("page", page);
            object.put("rowcount", 10);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getAssetsRepair(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<AssetRepair>>(this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<AssetRepair> result) {
                items = NListRefresh.getInstance().fjData(result,
                        mRefreshLayout,
                        items,
                        adapter,
                        AssetRepairActivity.this);
            }
        });
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.adapter = null;
        this.items = null;
    }
}
